package com.dao;

import com.bean.Student;
import com.bean.Teacher;
import com.bean.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by 程振奇 on 2015/9/16.
 */
@Service
public class UserDao {
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    public List<User> getUserById(User user){
        return sqlSessionTemplate.selectList(User.class.getName()+".getUserById",user);
    }

    public List<User> getUserByUserName(String userName){
        return sqlSessionTemplate.selectList(User.class.getName()+".getUserByUserName",userName);
    }
    public List<Student> getStudentByUserName(String userName){
        return sqlSessionTemplate.selectList("com.dao.StudentMapper.getStudentByUserName",userName);
    }
    public List<Teacher> getTeacherByUserName(String userName){
        return sqlSessionTemplate.selectList("com.dao.TeacherMapper.getTeacherByUserName",userName);
    }

    //通过sqlSessionTemplate查找对应的用户名和密码并返回
    public List<User> getUserByUsernameAndPass(User user) {
        return sqlSessionTemplate.selectList("com.bean.User.getUserByUsernameAndPass",user);
    }
    public List<Student> getStudentByUsernameAndPass(Student student) {
        return sqlSessionTemplate.selectList("com.dao.StudentMapper.getStudentByUsernameAndPass",student);
    }
    public List<Teacher> getTeacherByUsernameAndPass(Teacher teacher) {
        return sqlSessionTemplate.selectList("com.dao.TeacherMapper.getTeacherByUsernameAndPass",teacher);
    }

    //添加用户
    public void insertUser(User user){
        sqlSessionTemplate.insert("com.bean.User.insertUser",user);
    }
    public void insertUser(Student user){
        sqlSessionTemplate.insert("com.dao.StudentMapper.insertUser",user);
    }
    public void insertUser(Teacher user){
        sqlSessionTemplate.insert("com.dao.TeacherMapper.insertUser",user);
    }
    //用户名信息验证,验证用户名是否相同
    public List<User> getUsername(User user){
        return sqlSessionTemplate.selectList("com.bean.User.getUsername",user);
    }
    public List<Student> getUsername(Student student){
        return sqlSessionTemplate.selectList("com.dao.StudentMapper.getUsername",student);
    }
    public List<Teacher> getUsername(Teacher teacher){
        return sqlSessionTemplate.selectList("com.dao.TeacherMapper.getUsername",teacher);
    }
    //查询所有用户
    public List<User> getUserAll(){
        return sqlSessionTemplate.selectList("com.bean.User.getUserAll");
    }
    public List<Teacher> getTeacherAll(){
        return sqlSessionTemplate.selectList("com.dao.TeacherMapper.getTeacherAll");
    }
    public List<Student> getStudentAll(){
        return sqlSessionTemplate.selectList("com.dao.StudentMapper.getStudentAll");
    }
    //删除指定用户
    public void delUserByUserName(String userName){
        sqlSessionTemplate.delete("com.bean.User.delUserByUserName",userName);
    }
    public void delStudentByUserName(String userName){
        sqlSessionTemplate.delete("com.dao.StudentMapper.delStudentByUserName",userName);
    }
    public void delTeacherByUserName(String userName){
        sqlSessionTemplate.delete("com.dao.TeacherMapper.delTeacherByUserName",userName);
    }
    //修改用户信息
    public int updateUserByUserName(User user){
        return sqlSessionTemplate.update("com.bean.User.updateUserByUserName",user);
    }
    public int updateStudentByUserName(Student user){
        return sqlSessionTemplate.update("com.dao.StudentMapper.updateStudentByUserName",user);
    }
    public int updateTeacherByUserName(Teacher user){
        return sqlSessionTemplate.update("com.dao.TeacherMapper.updateTeacherByUserName",user);
    }

    //查询所有教师
    public List<Teacher> getAllTeacher(){
        return sqlSessionTemplate.selectList("com.dao.TeacherMapper.getAllTeacher");
    }

    //查询用户id
    public List<User> getIdByUserName(String userName){
        return sqlSessionTemplate.selectList("com.bean.User.getIdByUserName",userName);
    }
    public List<Student> getNotChoiceTeacherStudent(){
        return sqlSessionTemplate.selectList("com.dao.StudentMapper.getNotChoiceTeacherStudent");
    }
    public List<Student> getStudentById(Long id){
        return sqlSessionTemplate.selectList("com.dao.StudentMapper.getStudentById",id);
    }




}
